package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.ann.Log;
import com.itheima.mapper.StudentsMapper;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.pojo.dto.StudentsDto;
import com.itheima.service.StudentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentsServiceImpl implements StudentsService {

    @Autowired
    private StudentsMapper studentsMapper;

    public PageResult<Student> page(StudentQueryParam studentQueryParam){
        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());

        StudentsDto studentsDto = new StudentsDto();
        studentsDto.setDegree(studentQueryParam.getDegree());
        studentsDto.setClazzId(studentQueryParam.getClazzId());
        studentsDto.setName(studentQueryParam.getName());
        List<Student> students = studentsMapper.querySelect(studentsDto);

        Page<Student> page = (Page<Student>) students;

        return new PageResult<>(page.getTotal(),page.getResult());
    }

    /**
     * 保存学生信息
     * @param student
     */
    @Log
    public void save(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentsMapper.save(student);
    }

    /**
     * 删除
     * @param ids
     */
    @Log
    public void delete(List<Integer> ids) {
        studentsMapper.deleteBatch(ids);
    }

    /***
     * 根据id查询
     * @param id
     * @return
     */
    public Student getById(Integer id) {
        StudentsDto studentsDto = new StudentsDto();
        studentsDto.setId(id);
        List<Student> studentList = studentsMapper.querySelect(studentsDto);
        return studentList.get(0);
    }

    /**
     * 修改学员信息
     * @param student
     */
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentsMapper.update(student);
    }

    /**
     * 学员违纪处理
     * @param id
     * @param score
     */
    public void updateViolation(Integer id, short score) {
        Student student = new Student();
        student.setId(id);
        student.setViolationScore(score);
        student.setUpdateTime(LocalDateTime.now());
        studentsMapper.update(student);
    }
}
